package com.woniu.controller;

import com.woniu.pojo.PageBean;
import com.woniu.pojo.Permission;
import com.woniu.pojo.Role;
import com.woniu.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @RequestMapping("list")
    public String list(){
        return "role/list";
    }
    @RequestMapping("assignPermission")
    public String assignPermission(Integer rid,Model model){
        model.addAttribute("rid",rid);
        return "role/assignPermission";
    }
    @RequestMapping("listDo")
    @ResponseBody
    public Map<String,Object> listDo(PageBean pageBean, Role role){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            List<Role> rolesByPage = roleService.getRolersByPage(pageBean, role);
            resultMap.put("roleList",rolesByPage);
            resultMap.put("pageBean",pageBean);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    @RequestMapping("assignPermissionShowDo")
    @ResponseBody
    public List<Permission> assignPermissionShowDo(Integer rid){
        List<Permission> permissionList = roleService.getPermissionByRid(rid);
        return permissionList;
    }
}
